<template>
  <div class="smart-plan-table">
    <common-table
      ref="commonTable"
      :show-search-input="false"
      :fields="fields"
      :list-data="listData"
      :total="total"
      :list-loading="loading"
      @search="handleSearch"
    ></common-table>
  </div>
</template>

<script>
import CommonTable from '@src/modules/smartPlan/common/commonTable.vue'
import { getPageList } from '@src/modules/smartPlan/api/index.js'
import { t } from '@src/locales'
export default {
  name: 'smart-plan-table',
  props: {
    customerIds: {
      type: Array,
      default: () => []
    },
    productIds: {
      type: Array,
      default: () => []
    },
    contractIds: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      listData: [],
      loading: false,
      total: 0,
      fields: [{
        displayName: t('common.base.name'),
        fieldName: 'planName',
      }, {
        displayName: t('common.base.status'),
        fieldName: 'planStatus',
      }, {
        displayName: t('common.projectManage.taskTypeText'),
        fieldName: 'planTypeName',
      }, {
        displayName: t('smartPlan.createDimension'),
        fieldName: 'createDimension',
      }, {
        displayName: t('common.base.startTime'),
        fieldName: 'startSetting',
        formType: 'datetime',
      }, {
        displayName: t('common.base.recurrence'),
        fieldName: 'periodSetting',
      }, {
        displayName: t('common.base.endTime'),
        fieldName: 'endSetting',
        formType: 'datetime',
      }, {
        displayName: t('common.base.usualStatus.hasCreated'),
        fieldName: 'runNumber',
      }, {
        displayName: t('task.taskTypes.planTask.toBeCreate'),
        fieldName: 'waitNumber',
      }, {
        displayName: t('common.base.usualStatus.finish'),
        fieldName: 'successNumber',
      }, {
        displayName: t('common.base.createUser'),
        fieldName: 'createUserName',
      }, {
        displayName: t('common.base.createTime'),
        fieldName: 'createTime',
      }]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleSearch(params) {
      this.getList(params)
    },
    buildParams() {
      return {
        pageNum: 1,
        pageSize: 10,
      }
    },
    getList(params) {
      const searchParams = {
        ...this.buildParams(),
        ...params
      }
      if (this.customerIds.length) {
        searchParams.customerIds = this.customerIds
      }
      if (this.productIds.length) {
        searchParams.productIds = this.productIds
      }
      if (this.contractIds.length) {
        searchParams.contractIds = this.contractIds
      }
      this.loading = true
      getPageList(searchParams).then(res => {
        this.loading = false
        this.listData = res.data?.list || []
        this.total = res.data?.total || 0
      })
    },
  },
  components: {
    CommonTable,
  }
}
</script>

<style lang="scss">
.smart-plan-table {
  padding: 16px 0;
}
</style>